﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obout.Grid;
using HostingDomainWebsite;

namespace Hosting
{
    public partial class CategoryTitle : System.Web.UI.Page
    {

        protected SiteMaster master;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("AdminPage.aspx");
            }
            master = Master as SiteMaster;
            master.setCurrentNav("ServiceManagement", "ServiceTitle");
        }

        #region [Insert new Category Title]
        protected void gCateTitle_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.CategoryTitle title = new Hosting.Class.CategoryTitle();
            var isValid = title.addValid(e.Record["CTName"].ToString(), e.Record["CTDescription"].ToString());
            if (isValid == 1)
            {
                var isInsert = title.add(e.Record["CTName"].ToString(), e.Record["CTDescription"].ToString(), Boolean.Parse(e.Record["CTActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Insert new Category Title successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when insert Category Title!");
                }
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Name is greater than 2000 characters");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Description is greater than 2000 chareacters");
            }
        }
        #endregion

        #region [Update new Category Title]
        protected void gCateTitle_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.CategoryTitle title = new Hosting.Class.CategoryTitle();
            var isValid = title.editValid(e.Record["CTId"].ToString(), e.Record["CTName"].ToString(), e.Record["CTDescription"].ToString());
            if (isValid == 1)
            {
                var isUpdate = title.edit(e.Record["CTId"].ToString(), e.Record["CTName"].ToString(), e.Record["CTDescription"].ToString()
                    , Boolean.Parse(e.Record["CTActive"].ToString()));
                if (isUpdate == 0)
                {
                    master.getMessageSuccess("Update new Category Title successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when update Category Title!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Id is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Id is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Name is greater than 2000 characters");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Description is greater than 2000 chareacters");
            }
        }
        #endregion

        #region [Delete new Category Title]
        protected void gCateTitle_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.CategoryTitle title = new Class.CategoryTitle();
            var isDelete = title.delete(e.Record["CTId"].ToString());

            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete a category title!");
            }
        }
        #endregion
    }
}